package com.dmxh.controller;

import com.dmxh.params.QueryParams;
import com.dmxh.result.PageList;
import com.dmxh.result.Result;
import com.dmxh.service.SpecService;
import com.dmxh.vo.SpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecService service;

    @PostMapping("/queryPage")
    public Result<PageList<SpecificationVo>> queryPage(@RequestBody QueryParams<SpecificationVo> queryParams) {
        return new Result<PageList<SpecificationVo>>("1", "操作成功", service.queryPage(queryParams));
    }

    @DeleteMapping("/deleteInfo/{id}")
    public Result deleteInfo(@PathVariable Integer id) {
        service.deleteInfo(id);
        return Result.ok();
    }

    @GetMapping("/queryById/{id}")
    public Result<SpecificationVo> queryById(@PathVariable("id")long id) {
        return new Result<>("1", "操作成功", service.queryById(id));
    }

    @PostMapping("/save")
    public Result save(@RequestBody SpecificationVo specificationVo) {
        if (specificationVo.getId() == null) {
            service.addInfo(specificationVo);
        } else {
            service.updateInfo(specificationVo);
        }
        return Result.ok();
    }

    @PatchMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Integer[]ids) {
        service.deleteBatch(ids);
        return Result.ok();
    }

    @GetMapping("/queryAllSpec")
    public Result queryAllBrand() {
        return new Result("1", "操作成功",service.queryAllSpec());
    }
}
